/**
 * @Title: ParkingController.java
 * @Package com.qding.park.api.web.service.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2011 
 * Company:千丁互联
 * 
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:45:38
 * @version V1.0
 */

package com.qding.park.manager.rest.controller;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.core.exceptions.business.validator.RchlValidatorException;
import com.qding.park.common.core.exceptions.system.SystemUnKnown;
import com.qding.park.common.core.validate.RchlValidateBean;
import com.qding.park.common.core.validate.RchlValidateTools;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.common.web.session.SessionInfo;
import com.qding.park.manager.cache.ParkCacheInter;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.common.rest.enums.ResponseCodeEnum;
import com.qding.park.manager.facade.core.service.DemoFacade;
import com.qding.park.manager.facade.core.service.common.ParkCommonFacade;

/**
 * @ClassName: ParkingController
 * @Description: 车场 Controller
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:45:38
 */
@RestController
@RequestMapping("/parking")
public class ParkingController extends WebBaseController {
	private static final Logger _log = LoggerFactory.getLogger(ParkingController.class);
	
	@Autowired
	private DemoFacade demoFacade;

	@Autowired
	private ParkCacheInter parkCacheInter;
	
//	@Autowired
//	private  PayFeeFacade  payFeeFacade;
	
	@Autowired
	private  ParkCommonFacade  parkCommonFacade;
	

	/**
	 * @Description (查询用户名下所有车场信息,每个车场下面要有停泊车辆信息)
	 * @param userId
	 * @param phone
	 * @return 说明: １、根据用户id和手机号从数据库查询 关联的车场列表 2、根据车场信息可以知道车场软件供应商类型
	 *         及接口地址，通过接口地址查询车场的实时信息 ３、拼装报文返回
	 */
	@RequestMapping("/getParkings")
	public AnswerMsg getParkings(String userId, String phone, String projectId) {

		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("userId", userId);
			vb.getRequiredValidate().put("phone", phone);
			

			vb.getPhoneValidate().put("phone", phone);
			RchlValidateTools.validate(vb);
			/**************/

			ParkDataResult dataResult = null;
			if (!this.isDemoRequestByProject(projectId)) {
				this.clearSessionInfo(userId);
				//此处服务层里面有可能涉及到多种类型的车场,在这种情况下
				dataResult = parkCommonFacade.getParkings(userId, Long.valueOf(phone), projectId);
			} else {
				dataResult = demoFacade.getParkings(userId, phone, projectId);
				SessionInfo sessTemp = this.getSessionInfo(userId);
				if (sessTemp == null || sessTemp.getCreateDate() == null
						|| sessTemp.getCreateDate().getTime() < DateUtils.addHour(new Date(), -1).getTime()) {
					demoFacade.clearSession(userId);
				}
				
				/***** 保存会话信息 *********/
				List<Integer> parklist = parkCacheInter.bjproject2parkIds(projectId);
				SessionInfo session = new SessionInfo();
				session.setPhone(phone);
				session.setProjectId(projectId);
				session.setParklist(parklist);
				session.setCreateDate(new Date());
				this.saveSessionInfo(userId, session);
				/***** 保存会话信息 *********/
			}

			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(dataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {
			exceptionHandle.handle(e);// 异常处理
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		} catch (AdapterException e) {
			exceptionHandle.handle(e);// 接口异常
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e, ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
					"未知Exception", "userId=" + userId + ",phone=" + phone));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}

	}

	/**
	 * @Description (查询用户名下车场预定规则)
	 * @param parkId
	 * @return 流程:目前此处功能不走车场接口，直接从我们的数据库中取
	 */
	@RequestMapping("/parkBookRules")
	public AnswerMsg parkBookRules(String parkId) {

		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("parkId", parkId);
			
			vb.getIntValidate().put("parkId", parkId);
			RchlValidateTools.validate(vb);
			/**************/
			List<String> demoPark = systemConfig.calculationDemoPark();
			ParkDataResult dataResult = null;
			int parkInt = Integer.valueOf(parkId);
			if (demoPark.contains(parkId)) {
				// 演示请求
				dataResult = demoFacade.getParkBookRules(parkInt);
			} else {
				dataResult = parkCommonFacade.getParkBookRules(parkInt);
			}

			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(dataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		} catch (ServiceCoreException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e, ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
					"未知Exception", "parkId=" + parkId));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}

	}

	/**
	 * @Description (查询车辆所停车场)
	 * @param carNum
	 *            车牌
	 * @return 1、从数据库中根据车牌查询所有的登记车场供应商 ２、轮询每个车场
	 */
//	@RequestMapping("/queryParkByCar")
//	public AnswerMsg queryParkByCar(String carNum) {
//
//		try {
//			/****** 验证字段 ********/
//			RchlValidateBean vb = new RchlValidateBean();
//			vb.getRequiredValidate().put("carNum", carNum);
//
//			RchlValidateTools.validate(vb);
//			/**************/
//			
//			
//			ParkDataResult parkDataResult = parkFacade.queryParkingByCarnum(carNum);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
//			return answerMsg;
//		} catch (ServiceCoreException e) {
//			exceptionHandle.handle(e);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
//					e.getExceptionMessage().getName());
//			return answerMsg;
//		} catch (RchlValidatorException e) {
//
//			exceptionHandle.handle(e);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
//					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
//			return answerMsg;
//		} catch (Exception e) {
//			SystemUnKnown ex = new SystemUnKnown(e, ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
//					"未知Exception", "carNum=" + carNum));
//			exceptionHandle.handle(ex);
//			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
//			return answerMsg;
//		}
//	}

	@RequestMapping("/listall")
	public AnswerMsg listall(String phone, String userId, String projectId) {
		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("userId", userId);
			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult dataResult =null;
			if(this.isDemoRequestByProject(projectId)||this.isDemoRequest(userId)){
				//演示请求
				dataResult = this.demoFacade.listall(phone, userId, projectId);
			}else{
				//真实请求
				dataResult = parkCommonFacade.listall(phone, userId, projectId);
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(dataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		} catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			SystemUnKnown ex = new SystemUnKnown(e,
					ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name, "未知Exception", "未知Exception"));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}

}
